package com.hashcoding.eyes.data.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * LoginInfoList
 *
 * @author Maochuan
 * @date 2016-02-08
 */
public class LoginInfoList {
    private List<LoginInfo> mList;

    private static LoginInfoList inst;

    public static LoginInfoList instance() {
        if (inst == null) {
            inst = new LoginInfoList();
        }
        return inst;
    }

    private LoginInfoList() {
        mList = new ArrayList<>();

        initData();
    }

    public boolean login(String userID, String userPassword) {
        for (LoginInfo i : mList) {
            if (i.check(userID, userPassword)) return true;
        }
        return false;
    }

    private void initData() {
        mList.add(new LoginInfo("admin", "admin"));
        mList.add(new LoginInfo("mao", "123456"));
        mList.add(new LoginInfo("test", "test"));
    }

    public boolean insert(String userID, String userPassword) {
        for (LoginInfo i : mList) {
            if (i.getUserID().equals(userID)) return false;
        }
        mList.add(new LoginInfo(userID, userPassword));
        return true;
    }

    public boolean update(String userID, String userPassword) {
        for (LoginInfo i : mList) {
            if (i.getUserID().equals(userID)) {
                i.update(userID, userPassword);
                return true;
            }
        }
        return false;
    }


}
